package com.hc.portManage;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DevUdpPortManagement extends ConcurrentHashMap<String, Integer> {
	private static final long serialVersionUID = 1L;
	private static DevUdpPortManagement portManagement;
	private DevUdpPortManagement(){
	}
	
	public synchronized static DevUdpPortManagement getInstance(){
		if(portManagement == null)
			portManagement = new DevUdpPortManagement();
		return portManagement;
	}
	
	public void addDevId(String devId, int port){
		if(!containsKey(devId)){
			put(devId, port);
		}
	}
	
	public void updatePort(String devId, int port){
		if(containsKey(devId)){
			put(devId, port);
		}
	}
	
	public int getPort(String devId){
		return containsKey(devId) ? get(devId) : 0;
	}
	
	public String getDevId(int port){
		String devId = "";
		if(containsValue(port)){
			for(Map.Entry<String, Integer> entry : entrySet()){
				if(port == entry.getValue()){
					devId = entry.getKey();
					break;
				}
			}
		}
		return devId;
	}
}
